home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 49
/
Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso
/
-serious-
/
comms
/
other
/
novia
/
src
/
novia_visualeditor.c
< prev
next >
Wrap
Text File
|
1999-12-06
|
15KB
|
683 lines
#include <exec/lists.h>
#include <pragma/dos_lib.h>
#include <pragma/exec_lib.h>
#include <novia/novia_PortData.h>
#include <novia/novia_types.h>
#include <novia/novia_misc.h>
//#include <novia/novia_funcs.h>
#include <exec/memory.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <pragma/noviasys_lib.h>
struct line
{
line *ln_Succ;
line *ln_Pred;
char *text;
UWORD textsize; // real length of line
UWORD buffersize; // x bytes allocated memory
char linefeed;
};
struct linelist
{
struct line * lh_Head;
struct line * lh_Tail;
struct line * lh_TailPred;
};
/***********************************************************************/
struct line *VE_AllocateLine(ULONG len);
void VE_WriteLine(line *line);
void VE_FreeLine(struct line *freeline);
struct line *VE_AllocateLine(ULONG len)
{
struct line *newline;
if ((newline = (line *)AllocVec(sizeof(line), MEMF_ANY | MEMF_CLEAR)))
{
if ((newline->text = (char *)AllocVec(len+1, MEMF_ANY | MEMF_CLEAR)))
newline->buffersize = len;
else
{
FreeVec(newline);
newline=NULL;
}
}
return newline;
}
void VE_WriteLine(line *line)
{
Writeio(line->text,line->textsize);
if (line->text[line->textsize-1]!=10)
LF();
}
void VE_FreeLine(struct line *freeline)
{
Remove((Node *)freeline);
FreeVec(freeline->text);
FreeVec(freeline);
}
ULONG VisualEditor(char *filename, APTR data, ULONG flags)
{
struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
if (cport)
{
struct line *lineptr, // actual edited line
*newline, //
*startline, // first line in display
*endline; // last line in display
struct linelist llist;
struct List mylist;
ULONG lines = 0;
ULONG textsize = 0;
char cp[20];
char listnew = TRUE;
char quit = FALSE;
char *buffer = (char *)AllocVec(1024,MEMF_ANY);
ULONG ulong;
char oldX;
char result = TRUE;
ULONG CurserX;
ULONG CurserY;
ULONG ystart = 1;
ULONG yend = 1;
NewList((List *)&llist);
if (flags & VISUALEDIT_FILE)
{
BPTR fh;
if (buffer && ((fh = Open(filename,MODE_OLDFILE))))
{
ioprintf("reading file: %s\n",filename);
while (!quit && FGets(fh, buffer, 1024))
{
ulong = mystrlen(buffer);
if ((lineptr = VE_AllocateLine(ulong)))
{
CopyMem(buffer, lineptr->text, ulong);
lineptr->textsize = ulong;
if (buffer[ulong] == 10 | buffer[ulong] == 13)
{
lineptr->linefeed = TRUE;
textsize++;
}
AddTail((List *)&llist, (Node *)lineptr);
lines++;
textsize = textsize + lineptr->textsize;
if (lines <= 20)
{
endline = lineptr;
yend = lines;
}
}
else
{
ioprintf("unknown error. -125.99994582 \ncall Mircosoft Hotline (1)900-888-88888 ($3.99/min)\n");
quit = TRUE;
}
}
Close(fh);
}
else
{
if (flags & VISUALEDIT_EXITIFNOTEXIT)
{
ioprintf("Can't open %s\n",filename);
Writeio("Usage: EDITFILE filename\n",-1);
if (!buffer)
{
ioprintf("Can't open File, not anouth memory\n");
}
quit=TRUE;
}
else
{
if ((lineptr = VE_AllocateLine(20)))
{
AddTail((List *)&llist, (Node *)lineptr);
startline = lineptr;
lines++;
endline = lineptr;
yend = lines;
}
}
}
}
else
{
if (flags & VISUALEDIT_FROMADDRESS)
{
if (data)
{
char *ptr = data;
quit = FALSE;
while (!quit && *ptr)
{
ulong = mystrlen(ptr);
if ((lineptr = VE_AllocateLine(ulong)))
{
CopyMem(ptr, lineptr->text, ulong);
lineptr->textsize = ulong;
ptr = ptr + ulong;
if (ptr[ulong] == 10 | ptr[ulong] == 13)
{
lineptr->linefeed = TRUE;
textsize++;
}
while (*ptr && (*ptr == 10 | *ptr == 13))
ptr++;
AddTail((List *)&llist, (Node *)lineptr);
lines++;
textsize = textsize + lineptr->textsize;
if (lines <= 20)
{
endline = lineptr;
yend = lines;
}
}
else
quit = TRUE;
}
}
}
else
quit = TRUE;
}
if (!quit)
{
CLS();
ioprintf("z4c7 NOViA ViSUAL TEXT EDITOR V0.1beta z0\n");
ioprintf("z4c7 [ESC]ape Functionsmenu z0\n");
CurserX = 1;
CurserY = 1;
oldX = 1;
lineptr = llist.lh_Head;
startline= llist.lh_Head;
}
while(!quit && !cport->ProgramClose && cport->network.online)
{
if (listnew)
{
Writeio("HB",-1); // Jump to textbeginn. X=1; Y=3
ulong = 0;
newline = lineptr;
while (newline->ln_Succ && ulong<20)
{
VE_WriteLine(newline);
newline = newline->ln_Succ;
ulong++;
}
Writeio("HB",-1); // Jump to textbeginn. X=1; Y=3
listnew = FALSE;
}
Conread(cp,1,0);
/*** RETURN ***/
if (*cp == 13 | *cp == 10)
{
if ((newline = VE_AllocateLine(80)))
{
newline->textsize = lineptr->textsize - CurserX + 1;
newline->linefeed = lineptr->linefeed;
if (newline->textsize)
CopyMem(&lineptr->text[CurserX - 1], newline->text, newline->textsize); // Copy from Curser X to end of line
lineptr->text[CurserX - 1] = 0; // Insert LineFeed;
lineptr->textsize = CurserX - 1;
lineptr->linefeed = TRUE;
textsize++;
Insert((List *)&llist, (Node *)newline, (Node *)lineptr);
lineptr = newline;
if (CurserY < 15) // && yend < lines)
{
ioprintf("HM%dHL%s\r", CurserY + 4, lineptr->text);
CurserY++;
if (yend < 20)
{
yend++;
endline = (line *)llist.lh_TailPred;
}
else
{
if (endline->ln_Pred->ln_Pred)
endline = endline->ln_Pred;
}
}
else
{
if (yend < 20)
{
ioprintf("EL%s", lineptr->text);
endline = (line *)llist.lh_TailPred;
CurserY++;
}
else
{
if (startline->ln_Succ->ln_Succ)
startline = startline->ln_Succ;
if (endline->ln_Succ->ln_Succ)
endline = endline->ln_Succ;
ystart++;
ioprintf("HM%dHL%s\r", CurserY + 3, lineptr->text);
}
yend++;
}
CurserX = 1;
oldX = 1;
lines++;
}
}
/*** [ESC]ape ***/
if (*cp==27)
{
Writeio("Hz4c7 [Q]uit, [O]pen, [s]ave, [S]ave as, [F]ind/search z0\n",-1);
Conread(cp, 1, 0);
switch(*cp)
{
case 'Q': case 'q':
quit = TRUE;
CLS();
break;
case 's':
CLS();
{
BPTR fh;
if ((fh = Open(filename, MODE_NEWFILE)))
{
newline = llist.lh_Head;
quit = FALSE;
while(newline->ln_Succ && !(quit = FPuts(fh, newline->text)))
{
newline = newline->ln_Succ;
}
if (quit)PrintDosError(IoErr());
Close(fh);
}
else
PrintDosError(IoErr());
quit = TRUE;
}
break;
case 'S':
CLS();
{
BPTR fh;
if ((fh = Open("ram:test", MODE_NEWFILE)))
{
newline = llist.lh_Head;
quit = FALSE;
while(newline->ln_Succ && !(quit = FPuts(fh, newline->text)))
{
newline = newline->ln_Succ;
}
if (quit)PrintDosError(IoErr());
Close(fh);
}
else
PrintDosError(IoErr());
quit=TRUE;
}
break;
}
if (!quit)
Writeio("Hz4c7 [ESCape] Functionsmenu z0\n",-1);
}
/*** [BACKSPACE] ***/
if (*cp == 8)
{
if (CurserX > 1)
{
int i;
Writeio(cp,1);
CurserX--;
oldX = CurserX;
for (i = CurserX - 1; i < lineptr->textsize - 1; i++)
{
lineptr->text[i] = lineptr->text[i+1];
}
lineptr->textsize--;
lineptr->text[lineptr->textsize] = 0;
textsize--;
Writeio("P",-1);
}
}
/*** [DEL]ete ***/
if (*cp == 127)
{
if (CurserX <= lineptr->textsize)
{
int i;
for (i = CurserX - 1; i < lineptr->textsize - 1; i++)
{
lineptr->text[i] = lineptr->text[i + 1];
}
lineptr->textsize--;
lineptr->text[lineptr->textsize] = 0;
textsize--;
Writeio("P",-1);
}
}
/*** [CTRL] - [D], Kill to end of line ***/
if (*cp == 4)
{
if (CurserX <= lineptr->textsize)
{
textsize = textsize - (lineptr->textsize - (CurserX - 1));
lineptr->text[CurserX - 1] = 0;
lineptr->textsize = CurserX - 1;
Writeio("",-1);
}
}
/*** [CTRL] - [K], delete line ***/
if (*cp == 11)
{
if (lineptr->ln_Succ->ln_Succ)
{
CurserX = 1;
oldX = CurserX;
Writeio("M",-1);
newline = lineptr->ln_Succ;
if (startline == lineptr)
startline = newline;
if (lines - yend > 20)
{
if (endline->ln_Succ->ln_Succ)
{
endline = endline->ln_Succ;
ioprintf("H%s%dH",endline->text,CurserY + 3);
}
}
else
yend--;
textsize = textsize - lineptr->textsize;
if (lineptr->linefeed)
textsize--;
VE_FreeLine(lineptr);
lineptr = newline;
lines--;
}
}
/*** ASCII KEYS ***/
if (isgraph(*cp) | *cp == ' ' && lineptr->textsize < 80)
{
if (lineptr->textsize + 3 > lineptr->buffersize)
{
char *newtext = (char *) AllocVec(lineptr->textsize +20 , MEMF_ANY | MEMF_CLEAR);
if (newtext)
{
CopyMem(lineptr->text, newtext, lineptr->textsize);
FreeVec(lineptr->text);
lineptr->text = newtext;
lineptr->buffersize = lineptr->textsize + 20;
}
}
if (lineptr->textsize + 1 < lineptr->buffersize)
{
if (CurserX == lineptr->textsize + 1)
{
Writeio(cp,1);
lineptr->text[lineptr->textsize] = *cp;
lineptr->text[lineptr->textsize+1] = 0;
lineptr->textsize++;
CurserX++;
oldX = CurserX;
}
else
{
Writeio("@",-1);
Writeio(cp,1);
memmove(&lineptr->text[CurserX], &lineptr->text[CurserX - 1], (lineptr->textsize - CurserX) + 1);
lineptr->text[CurserX - 1] = *cp;
lineptr->text[lineptr->textsize + 1] = 0;
lineptr->textsize++;
CurserX++;
oldX = CurserX;
}
textsize++;
}
}
if (*cp==-101) // beginning Curser Keys
{
Conread(cp,1,0);
switch (*cp)
{
/*** CURSER LEFT ***/
case 'D':
if (CurserX > 1)
{
Writeio("D",-1);
CurserX--;
}
else
{
if (lineptr->ln_Pred->ln_Pred)
{
lineptr = lineptr->ln_Pred;
if (CurserY <= 5)
{
if (startline->ln_Pred->ln_Pred)
{
startline = startline->ln_Pred;
Writeio("HMHL",-1);
VE_WriteLine(startline);
sprintf(buffer,"%dH",CurserY + 3);
Writeio(buffer,-1);
yend--;
endline = endline->ln_Pred;
}
else
{
LF();
Writeio("A",-1);
CurserY--;
}
}
else
{
LF();
Writeio("A",-1);
CurserY--;
}
if (lineptr->textsize)
{
cright(lineptr->textsize);
CurserX = lineptr->textsize + 1;
}
else
CurserX = 1;
}
}
oldX = CurserX;
break;
/*** CURSER RIGHT ***/
case 'C':
if (CurserX <= lineptr->textsize)
{
Writeio("C",-1);
CurserX++;
}
else
{
if (lineptr->ln_Succ->ln_Succ)
{
lineptr = lineptr->ln_Succ;
if (CurserY >= 15)
{
if (endline->ln_Succ->ln_Succ)
{
startline = startline->ln_Succ;
endline = endline->ln_Succ;
Writeio("HMH",-1);
VE_WriteLine(endline);
sprintf(buffer,"%dH", CurserY + 3);
Writeio(buffer,-1);
yend++;
}
else
{
LF();
CurserY++;
}
}
else
{
LF();
CurserY++;
}
CurserX = 1;
}
}
oldX = CurserX;
break;
/*** CURSER UP ***/
case 'A':
if (lineptr->ln_Pred->ln_Pred)
{
lineptr = lineptr->ln_Pred;
if (CurserY <= 5)
{
if (startline->ln_Pred->ln_Pred)
{
startline = startline->ln_Pred;
if (yend > 20)
endline = endline->ln_Pred;
Writeio("HMHL",-1);
VE_WriteLine(startline);
sprintf(buffer,"%dH",CurserY + 3);
Writeio(buffer,-1);
yend--;
ystart--;
}
else
{
Writeio("F",-1);
CurserY--;
}
}
else
{
Writeio("F",-1);
CurserY--;
}
if (oldX > lineptr->textsize)
{
if (lineptr->textsize)
cright(lineptr->textsize);
CurserX = lineptr->textsize + 1;
}
else
{
if (oldX > 1)
cright(oldX - 1);
CurserX = oldX;
}
// printf("Hlineptr: %-80s\n",lineptr->text);
}
break;
/*** CURSER DOWN ***/
case 'B':
if (lineptr->ln_Succ->ln_Succ)
{
lineptr = lineptr->ln_Succ;
if (CurserY >= 15)
{
if (endline->ln_Succ->ln_Succ)
{
startline = startline->ln_Succ;
endline = endline->ln_Succ;
Writeio("HMH",-1);
VE_WriteLine(endline);
sprintf(buffer,"%dH",CurserY + 3);
Writeio(buffer,-1);
yend++;
ystart++;
}
else
{
Writeio("E",-1);
CurserY++;
}
}
else
{
Writeio("E",-1);
CurserY++;
}
if (oldX > lineptr->textsize)
{
if (lineptr->textsize)
cright(lineptr->textsize);
CurserX = lineptr->textsize + 1;
}
else
{
if (oldX > 1)
cright(oldX - 1);
CurserX = oldX;
}
// printf("Hlineptr: %-80s\n",lineptr->text);
}
break;
}
}
// ioprintf("pHz4c7 X: %02d Y: %02d lines: %04d (%02d:%02d) sl: <%s>\n pt: <%s>\n el: <%s>z0%d;%dHp",CurserX,CurserY,lines,ystart,yend,startline->text,lineptr->text,endline->text,CurserY + 3,CurserX);
ioprintf("pHz4c7 X: %02d Y: %02d lines: %04d ystart: %04d yend: %04d textsize: %05dz0%d;%dHp",CurserX,CurserY,lines,ystart,yend,textsize,CurserY + 3,CurserX);
}
if (buffer)FreeVec(buffer);
while (!IsListEmpty((List *)&llist))VE_FreeLine(llist.lh_Head);
return result;
}
}
int mystrlen(char *str)
{
char *ptr = str;
while (*ptr != 0 && *ptr != 10 && *ptr != 13)
ptr++;
return(ptr - str);
}